package com.witmore.skyline.web.service.third.yjx.client;

import com.alibaba.fastjson.JSONObject;
import com.witmore.skyline.web.annotation.ApiCache;
import com.witmore.skyline.web.service.third.constants.ApiClientCommonConstant;
import com.witmore.skyline.web.service.third.yjx.dto.DeviceLocusDTO;
import com.witmore.skyline.web.service.third.yjx.dto.WorkDetailDTO;
import com.witmore.skyline.web.service.third.yjx.dto.WorkTimeListDTO;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.stereotype.Service;

import jakarta.validation.constraints.NotNull;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import static java.lang.String.format;

/**
 * Description: ApiDeviceDataClient
 * Author: 豆子高
 * Date: 2025/2/25 14:12
 */
@Slf4j
@Service
public class ApiDeviceDataClient {

    /**
     * 查询设备指定日期的工作详情
     */
    @ApiCache(expireMinutes = 5, keyParams = {"deviceSn", "date"})
    public List<WorkDetailDTO> workTimeDetails(String deviceSn, String date) {
        String urlString = format(ApiClientCommonConstant.API_PREFIX + ApiClientCommonConstant.GET_DEVICE_WORK_TIME_PATH,
                TokenCache.getToken(), deviceSn, date);
        String responseResult = ApiClient.responseHandle(urlString, "查询设备指定日期的工作详情接口");
        if (ObjectUtils.isEmpty(responseResult)) {
            log.info("api 查询设备指定日期的工作详情接口调用结果为空");
            return new ArrayList<>();
        }
        return JSONObject.parseArray(responseResult, WorkDetailDTO.class);
    }

    /**
     * 查询设备指定日期的工作详情带状态
     */
    @ApiCache(expireMinutes = 5, keyParams = {"deviceSn", "date"})
    public List<WorkDetailDTO> workTimeDetailWithStatus(String deviceSn, String date) throws Exception {
        String urlString = format(ApiClientCommonConstant.API_PREFIX + ApiClientCommonConstant.GET_DEVICE_WORK_TIME_STATUS_PATH,
                TokenCache.getToken(), deviceSn, date);
        String responseResult = ApiClient.responseHandle(urlString, "查询设备指定日期的工作详情带状态接口");
        if (ObjectUtils.isEmpty(responseResult)) {
            log.info("api 查询设备指定日期的工作详情带状态接口调用结果为空");
            return new ArrayList<>();
        }
        return JSONObject.parseArray(responseResult, WorkDetailDTO.class);
    }

    /**
     * 查询设备工时
     */
    @ApiCache(expireMinutes = 5, keyParams = {"deviceSn", "startDay", "endDay"})
    public List<WorkTimeListDTO> workTimeList(String deviceSn, @NotNull String startDay, @NotNull String endDay) {
        String urlString = format(ApiClientCommonConstant.API_PREFIX + ApiClientCommonConstant.GET_DEVICE_WORK_TIME_LIST_PATH,
                TokenCache.getToken(), deviceSn, startDay, endDay);
        String responseResult = ApiClient.responseHandle(urlString, "查询设备工时接口");
        if (ObjectUtils.isEmpty(responseResult)) {
            log.info("api 查询设备工时接口调用结果为空");
            return new ArrayList<>();
        }
        // 处理工时 由于第三方返回的 workTime 工时算法陈旧 需要处理
        List<WorkTimeListDTO> workTimeListDTOS = JSONObject.parseArray(responseResult, WorkTimeListDTO.class);
        for (WorkTimeListDTO workTimeListDTO : workTimeListDTOS) {
            BigDecimal loadWorkTime = null == workTimeListDTO.getLoadWorkTime() ? BigDecimal.ZERO : workTimeListDTO.getLoadWorkTime();
            BigDecimal idleTime = null == workTimeListDTO.getIdleTime() ? BigDecimal.ZERO : workTimeListDTO.getIdleTime();
            BigDecimal workTime = loadWorkTime.add(idleTime);
            workTimeListDTO.setWorkTime(workTime);
        }
        return workTimeListDTOS;
    }


    /**
     * 获取设备经纬度
     */
    @ApiCache(expireMinutes = 5, keyParams = {"deviceSn", "startTime", "endTime"})
    public List<DeviceLocusDTO> getDeviceLocus(String deviceSn, String startTime, String endTime) {
        String urlString = format(ApiClientCommonConstant.API_PREFIX + ApiClientCommonConstant.GET_DEVICE_LOCUS_PATH,
                TokenCache.getToken(), deviceSn, startTime, endTime);
        String responseResult = ApiClient.responseHandle(urlString, "获取设备经纬度接口");
        if (ObjectUtils.isEmpty(responseResult)) {
            log.info("api 获取设备经纬度接口调用结果为空");
            return new ArrayList<>();
        }
        return JSONObject.parseArray(responseResult, DeviceLocusDTO.class);
    }

}
